﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace jnxc.Web.controlinfo
{
    /// <summary>
    /// GetControl 的摘要说明
    /// </summary>
    public class GetControl : IHttpHandler, IRequiresSessionState
    {

        StringBuilder result = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        jnxc.BLL.controlinfo bll = new BLL.controlinfo();
        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "query")
                Query(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);

        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["cid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();

            }

        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string cname = context.Request.Form["cname"];
            int cid = int.Parse(context.Request.Form["cid"]);
            string cunit = context.Request.Form["cunit"];

            jnxc.Model.controlinfo model = new jnxc.Model.controlinfo();
            model.cname =cname;
            model.cid = cid;
            model.cunit = cunit;
            if (bll.Update(model))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();

            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";            
           
            if (Web.Admin.myinfo.isLogin(context))
            {
                int parentid = 0;
                if (context.Request.Form["parentid"] != "")
                    parentid = int.Parse(context.Request.Form["parentid"]);
                string cname = context.Request.Form["cname"];
                string cunit = context.Request.Form["cunit"];
                jnxc.Model.controlinfo model = new jnxc.Model.controlinfo();
                model.cname = cname;
                model.parentid = parentid;
                model.cunit = cunit;
                if (bll.Add(model)>0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        private string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol,string tag, string rela, object pId)
        {
            result.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {
                sb.Append("[");
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"tag\":\"" + row[tag] + "\"");
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            //点击展开
                            sb.Append(",\"children\":");
                            GetTreeJsonByTable(tabel, idCol, txtCol,tag, rela, row[idCol]);
                            result.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        result.Append(sb.ToString());
                        sb.Remove(0, sb.Length); ;
                        sb.Append("},");
                    }
                    sb = sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]");
                result.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return result.ToString();
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";            
            DataSet ds = bll.GetAllList();

            string str = GetTreeJsonByTable(ds.Tables[0], "cid", "cname","cunit", "parentid", 0);
            context.Response.Write(str);//返回给前台页面  
            context.Response.End();

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}